package com.zxh.utils;


import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class DBUtils {
	public static String username="root";
	public static String password="";
	public static String url="jdbc:mysql://127.0.0.1:3306/offer?useUnicode=true&characterEncoding=utf8";
	public static String driver="com.mysql.jdbc.Driver";
	
	static{
		try {
			Class.forName(driver);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}//加载驱动
	}
	
	/**
	 * 获取数据库连接
	 * @throws SQLException 
	 */
	
	public static Connection getConnection() throws SQLException {
		return DriverManager.getConnection(url, username, password);
	}
	
	/**
	 * 关闭数据库连接
	 */
	
	public static void close(Connection con,ResultSet rs,Statement st,PreparedStatement ps){
		try {
			
			if(rs!=null){
				rs.close();
			}
			
			if(st !=null){
				st.close();
			}
			
			if(ps!=null){
				ps.close();
			}
			
			if(con!=null){
				con.close();
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	public static void main(String[] args) {
		try {
			getConnection();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
